package com.yict.csms.resourceplan.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.BaseDao;
import com.yict.csms.resourceplan.dao.IBoxContractorDao;
import com.yict.csms.resourceplan.entity.BoxContractor;
import com.yict.csms.system.entity.DataDict;
import com.yict.csms.system.entity.User;

@Repository("boxContractorDao")
public class BoxContractorDaoImpl extends BaseDao<BoxContractor, Long> implements IBoxContractorDao {
	
	public String getMaxWorkNo(Date date) {
		String result = "";
		String queryString = "select max(t.workNo) from BoxContractor t where 1=1 and t.inputDate = :date ";
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setParameter("date", date);
			List<String> list = query.list();
			if (list != null && list.size() > 0) {
				result = list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public BoxContractor fingByWorkNo(String workNo) {
		BoxContractor boxContractor = null;
		String hql = "from BoxContractor t where 1=1 and t.workNo = :workNo";	
		try {
			Query query = this.getSession().createQuery(hql);
			query.setParameter("workNo", workNo);
			List<BoxContractor> list = query.list();
			if (list != null && list.size() > 0) {
				boxContractor = list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return boxContractor;
	}

	@Override
	public BoxContractor fingWorkNoByBoxNum(String boxNum) {
		BoxContractor boxContractor = null;
		String hql = "from BoxContractor t where 1=1 and t.bcId = (select b.bcId from BoxContractorLine b where 1=1 and b.boxNum =:boxNum)";	
		try {
			Query query = this.getSession().createQuery(hql);
			query.setParameter("boxNum", boxNum);
			List<BoxContractor> list = query.list();
			if (list != null && list.size() > 0) {
				boxContractor = list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return boxContractor;
	}

	@Override
	public boolean commitAll(List<Long> bcIds, DataDict status, User us, Date date) {
		boolean flag = false;
		String hql = "update BoxContractor t set t.status.dictid =:status, t.confirmPerson =:us, t.confirmDate =:date where 1=1 and t.bcId in (:bcIds)";	
		try {
			Query query = this.getSession().createQuery(hql);
			query.setParameter("status", status.getDictid());
			query.setParameter("us", us);
			query.setParameter("date", date);
			query.setParameterList("bcIds", bcIds);
			query.executeUpdate();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean backAssigned(List<Long> bcId, DataDict status, User us, Date date) {
		boolean flag = false;
		String hql = "update BoxContractor t set t.status.dictid =:status, t.reAssignedPerson =:us, t.reAssignedDate =:date, t.assignedPerson = null, t.assignedDate = null" +
				" where 1=1 and t.bcId in (:bcId)";	
		try {
			Query query = this.getSession().createQuery(hql);
			query.setParameter("status", status.getDictid());
			query.setParameter("us", us);
			query.setParameter("date", date);
			query.setParameterList("bcId", bcId);
			query.executeUpdate();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	@Override
	public boolean backAssigned(Long bcId, DataDict status, User us) {
		boolean flag = false;
		String hql = "update BoxContractor t set t.status.dictid =:status, t.reConfirmPerson =:us, t.reConfirmDate =:date, t.confirmPerson = null, t.confirmDate = null" +
				" where 1=1 and t.bcId in (:bcId)";	
		try {
			Query query = this.getSession().createQuery(hql);
			query.setParameter("status", status.getDictid());
			query.setParameter("us", us);
			query.setParameter("date", new Date());
			query.setParameter("bcId", bcId);
			query.executeUpdate();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}
